package cc.wsyw126.java.nowcoder.offerdidi;

import java.util.*;

/**
 * Created by dp on 17/3/30.
 */
public class didi2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            String[] split = s.split(" ");
            int n = Integer.valueOf(split[0]), m = Integer.valueOf(split[1]);
            HashMap<String, Set<Integer>> map = new HashMap<>();
            String[] value = new String[n];
            for (int i = 0; i < n; i++) {
                String s1 = sc.nextLine();
                String[] split1 = s1.split(" ");
                for (String s2 : split1) {
                    String s3 = s2.toLowerCase();
                    Set<Integer> integers = map.get(s3);
                    if (integers == null) {
                        integers = new HashSet<>();
                        integers.add(i);
                        map.put(s3, integers);
                    } else {
                        integers.add(i);
                    }
                }
                value[i] = s1;
            }

            for (int i = 0; i < m; i++) {
                int position = 0;
                HashMap<Integer, Integer> temp = new HashMap<>();
                String s1 = sc.nextLine();
                String[] split1 = s1.split(" ");
                HashSet<String> objects = new HashSet<>();
                for (String s2 : split1) {
                    String s3 = s2.toLowerCase();
                    objects.add(s3);
                }
                for (String s2 : objects) {
                    Set<Integer> integers = map.get(s2);
                    if (integers != null) {
                        for (Integer integer : integers) {
                            Integer integer1 = temp.get(integer);
                            if (integer1 == null) {
                                temp.put(integer, 1);
                            } else {
                                temp.put(integer, temp.get(integer) + 1);
                            }
                        }
                    }
                }
                int count = 0;
                for (Map.Entry<Integer, Integer> integerIntegerEntry : temp.entrySet()) {
                    if (integerIntegerEntry.getValue() > count) {
                        position = integerIntegerEntry.getKey();
                        count = integerIntegerEntry.getValue();
                    }
                }
                System.out.println(value[position]);

            }
        }
    }
}
